<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <jsp:include page="__header.jsp"/>
    <style>
        #table .highlight td{background-color: antiquewhite;}
        td{ cursor: pointer;}
           body {
               min-height: 100vh;
               width: 100%;
           }
    </style>
</head>
<body>
<div class="card">
    <!--标题-->
    <h5 class="card-header" id="title">${entityData.entityComment}管理 ${condition}</h5>

        <div class="row">
            <c:if test="${not entityData.viewOnly}">
            <div class="col-1">
                <!--添加按钮，可以打开悬浮窗-->
                <button type="button" class="btn btn-sm btn-primary"  data-bs-toggle="modal" data-bs-target="#backDropModal" onclick="add()"  id="addButton" >
                    <i class="fa fa-plus"></i> 添加
                </button>
            </div>

            <div class="col-1">
                <!--修改按钮，可以打开悬浮窗-->
                <button class="btn btn-sm  btn-warning" data-bs-toggle="modal" data-bs-target="#backDropModal" onclick="edit()" id="editButton"  disabled>
                    <i class="fa fa-pencil"></i> 修改
                </button>
            </div>

            <div class="col-1">
                <!--删除按钮-->
                <button class="btn btn-sm  btn-danger"  id="delButton" disabled>
                    <i class="fa fa-trash"></i> 删除
                </button>
            </div>
            </c:if>
            <div class="col-3">
                <!--搜索按钮，可以打开悬浮窗-->
                <button class="btn btn-sm  btn-success"  id="searchButton" >
                    <i class="fa fa-search"></i> 搜索
                </button>
                <input type="text" id="searchInput">
            </div>
            <div class="col-1">
                <!--筛选搜索弹窗区-->
                <div class="btn-group dropend">
                    <button class="btn btn-sm dropdown-toggle btn-info "  id="filterButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="fa fa-filter"></i> 高级筛选
                    </button>
                    <div class="dropdown-menu dropdown-menu-end w-px-300" id="sqlInputDiv" style="">
                        <label for="sqlInput" class="form-label">筛选逻辑语句：</label>
                        <input type="text" id="sqlInput" readonly>
                        <br>
                        <button class="btn btn-sm btn-info sqlButton" sqlValue="并且">&amp;&amp; 并且</button>
                        <button class="btn btn-sm btn-info sqlButton"  sqlValue="或者">|| 或者</button>
                        <br>
                        <button class="btn btn-sm btn-primary sqlButton"  sqlValue="(">&#40; 左括号</button>
                        <button class="btn btn-sm btn-primary sqlButton" sqlValue=")">&#41; 右括号</button>
                        <button class="btn btn-sm btn-primary sqlButton"  sqlValue="=">= 等于</button>
                        <button class="btn btn-sm btn-primary sqlButton"  sqlValue="&gt;">&gt; 大于</button>
                        <button class="btn btn-sm btn-primary sqlButton"  sqlValue="&lt;">&lt; 小于</button>
                        <button class="btn btn-sm btn-primary sqlButton"  sqlValue="&ge;">&ge; 大于等于</button>
                        <button class="btn btn-sm btn-primary sqlButton" sqlValue="&le;">&le; 小于等于</button>
                        <input type="text" class="form-control" id="sqlValueInput" placeholder="值...">
                        <button class="btn btn-sm btn-primary sqlButton" sqlValue=" ">  插值</button>
                        <br>
                        <c:forEach items="${entityData.fields}" var="field">
                            <button class="btn btn-sm btn-secondary sqlButton"  sqlValue="${field.fieldComment}">${field.fieldComment}</button>
                        </c:forEach>
                        <br>
                        <button class="btn btn-sm btn-danger"  onclick="$('#sqlInput').val('');">清空</button>
                    </div>
                </div>
            </div>
            <div class="col-3">
                <div class="progress">
                    <div id="progressBar" class="progress-bar progress-bar-striped progress-bar-animated bg-success" role="progressbar" style="width: 0%" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
                    <span id="resource_loaded">0 </span>
                    /
                    <span id="resource_total"> </span>
                </div>
                <div class="spinner-border text-primary" id="loadingSpinner" role="status">
                    <span class="visually-hidden">载入中..</span>
                </div>
            </div>
            <!--增删改查按钮、表格区-->
            <div class="row">
                <div class="table-responsive text-nowrap"><br>
                    <table class="table table-hover table-sm " id="table">
                        <thead>
                        <tr id="tableHeaderRow">
                            <c:forEach items="${entityData.fields}" var="field">
                                <th fieldName="${field.fieldName}"
                                    fieldComment = "${field.fieldComment}"
                                    typeClassName= "${field.typeClassName}"
                                    typeClassShortName= "${field.typeClassShortName}"
                                    isIdentity= "${field.isIdentity}"
                                    isAutoIncrement= "${field.isAutoIncrement}"
                                    isNullable= "${field.isNullable}"
                                    isMoney="${field.money}"
                                    hasForeignKey= "${field.hasForeignKey}"
                                >
                                        ${field.fieldComment}
                                </th>
                            </c:forEach>

                        </tr>
                        </thead>
                        <tbody class="table-border-bottom-0" id="tableBody">


                        </tbody>
                    </table>

                </div>
            </div>




        </div>



</div>
<!--添加、修改弹窗区-->
<c:if test="${not entityData.viewOnly}">
<div class="modal fade" id="backDropModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="modalTitle" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content" id="form">
            <div class="modal-header">
                <h5 class="modal-title" id="modalTitle">${entityData.entityComment}管理</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="modalForm">
                <input name="entityClassName" value="${entityClassName}" class="d-none">
            <div class="modal-body" id="modalBody">
            <c:forEach items="${entityData.fields}" var="field">
                <c:choose>
                    <%--如果这个字段是自增ID，就不允许输入--%>
                    <c:when test = "${field.isAutoIncrement}">
                        <input type="text"
                               id="${field.fieldName}_modalInput"
                               name="${field.fieldName}"
                               class="d-none"
                               placeholder="${field.fieldComment}"
                               value="">
                    </c:when>
                    <%--如果这个字段是用户，不允许输入，选择当前用户--%>
                    <c:when test = "${field.fieldComment.contains('用户')}">
                        <div class="row">
                            <div class="col mb-3">
                                <label for="${field.fieldName}_modalInput" class="form-label">${field.fieldComment} 管理员</label>
                                <input type="text"
                                       id="${field.fieldName}_modalInput"
                                       name="${field.fieldName}"
                                       class="d-none"
                                       placeholder="${field.fieldComment}"
                                       value="admin" readonly/>
                            </div>
                        </div>

                    </c:when>
                    <%-- 如果这个实体类是生成ID，只有添加时生成，不允许修改 --%>
                    <c:when test="${field.fieldName eq 'id' and not empty generatedOrderId}">
                        <div class="row">
                            <div class="col mb-3">
                                <label for="${field.fieldName}_modalInput" class="form-label">${field.fieldComment}（自动生成）</label>
                                <input type="text"
                                       id="generatedIdInput"
                                       name="${field.fieldName}"
                                       class="auth-input post-put-input"
                                       value="${generatedOrderId}" readonly>
                            </div>
                        </div>
                    </c:when>
                    <%-- 如果这个字段是日期 Date --%>
                    <c:when test="${field.date}">
                        <div class="row">
                            <div class="col mb-3">
                                <label for="${field.fieldName}_modalInput" class="form-label">${field.fieldComment}</label>
                                <input type="date"
                                       value="${dateToday}"
                                       id="${field.fieldName}_modalInput"
                                       name="${field.fieldName}"
                                       class="form-control post-put-input"
                                >
                            </div>
                        </div>
                    </c:when>
                    <%-- 如果这个字段是外键--%>
                    <c:when test="${field.hasForeignKey}">
                        <div class="row">
                            <div class="col mb-3">
                                <%--如果有条件则限定条件--%>
                                <label for="${field.fieldName}_modalInput" class="form-label">${field.fieldComment}</label>
                                <c:choose>
                                    <c:when test="${not empty condition   and   conditionName eq field.fieldName   and   not empty conditionValue}">
                                        <input type="text"
                                               value="${conditionValue}"
                                               id="${field.fieldName}_modalInput"
                                               name="${field.fieldName}"
                                               class="form-control post-put-input"
                                               placeholder="${conditionValue}" readonly>
                                    </c:when>
                                    <c:otherwise>
                                        <select class="js-example-basic-single post-put-input" name="${field.fieldName}"
                                                id="select2_${field.fieldName}"
                                                needLoadDropdownMenu="true"
                                                isGeneratedId="${field.typeClassGenerateId}"
                                                typeClassShortName="${field.typeClassShortName}">
                                        </select>
                                    </c:otherwise>
                                </c:choose>


                            </div>
                        </div>
                    </c:when>
                    <c:otherwise>
                        <div class="row">
                            <div class="col mb-3">
                                <label for="${field.fieldName}_modalInput" class="form-label">${field.fieldComment}</label>
                                <input type="text"
                                       value=""
                                       id="${field.fieldName}_modalInput"
                                       name="${field.fieldName}"
                                       class="form-control post-put-input"
                                       placeholder="${field.fieldComment} ${field.isNullable?'可空':''}">
                            </div>
                        </div>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
            </div>
            </form>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-outline-primary" onclick="sendModalData()">提交</button>
            </div>

        </div>
    </div>
</div>
</c:if>

</body>
<jsp:include page="__footer.jsp"/>
<script src="crud_event.js"></script>
<script src="crud_utils.js"></script>
<script id="tableJs"
        condition="${condition}"
        entityClassName = "${entityClassName}"
        entityTableName="${entityData.entityTableName}"
        requestUrl="${tableContentRequestUrl}"
        entityData="${entityData.toEscapedString()}"
        goid="${generatedOrderId}"
        src="crud_table.js" ></script>

</html>
